
from service.api.wework_api import WeWork


class Tag(WeWork):


    def search(self):
        data = dict(
            url="https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list",
            method='POST',
            params={"access_token": self.token},
            json={}
        )

        return self.request(data)

    def add(self, tag_name, group_name):
        data = {
            'url': "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag",
            'method': 'POST',
            'params': {"access_token": self.token},
            'json': {
                "group_name": group_name,
                "tag": [
                    {
                        "name": tag_name
                    }
                ]
            }
        }
        resp = self.request(data)
        return resp

    def delete(self, tag_id):
        data = {
            'url': "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag",
            'params': {"access_token": self.token},
            'json': {
                "tag_id": [
                    tag_id
                ]
            }
        }

        return self.request(data)


    def clear_data(self):
        """
        数据清理
        :return:
        """
        data = self.search()
        tag_ids = [tag['id'] for group in data.json()['tag_group'] for tag in group['tag']]
        resp = self.delete(tag_ids)
        return resp
